package com.ai.utils;


import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fengs
 * @description 通用工具类
 * @date 2022/7/7 17:01
 */
public class CommonUtil {

    /**
     * 获取IP
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
}
